package com.atf.atfconfigurator.views;

import org.apache.commons.configuration.PropertiesConfiguration;

import com.atf.atfconfigurator.AtfconfiguratorUI;
import com.atf.atfconfigurator.composite.CollapseableBox;
import com.vaadin.data.Property;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.navigator.View;
import com.vaadin.navigator.ViewChangeListener.ViewChangeEvent;
import com.vaadin.ui.Component;
import com.vaadin.ui.FormLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;

public class SharedLibraryView extends VerticalLayout implements View{
	private PropertiesConfiguration wasConfigProperty;
	@Override
	public void enter(ViewChangeEvent event) {
		// TODO Auto-generated method stub
		wasConfigProperty=((AtfconfiguratorUI) getUI()).getWasConfiguration();	
		setSpacing(true);
        setMargin(true);

        Label title = new Label(wasConfigProperty.getString("appName"));
        title.addStyleName("h2");
        title.addStyleName("colored");
        addComponent(title);
        
		final FormLayout form = new FormLayout();
        form.setMargin(false);
        form.setSpacing(true);
        form.setWidth("100%");
        //form.set
        form.addStyleName("light");
        addComponent(form);
        
        final TextField webSphereSharedLibrary = new TextField("Number of Shared Library ");
	     int webSphereSharedLibraryValue=wasConfigProperty.getInt("webSphereSharedLibrary",0);
	     webSphereSharedLibrary.setValue(String.valueOf(webSphereSharedLibraryValue));
	     webSphereSharedLibrary.setWidth("50%");
	     form.addComponent(webSphereSharedLibrary);
	     
	     webSphereSharedLibrary.addValueChangeListener(new Property.ValueChangeListener(){

				/**
				 * 
				 */
				private static final long serialVersionUID = 1L;

				@Override
				public void valueChange(ValueChangeEvent event) {
					// TODO Auto-generated method stub
					((AtfconfiguratorUI) getUI()).getWasConfiguration().setProperty("webSphereSharedLibrary",  (String) event.getProperty().getValue());
					((AtfconfiguratorUI) getUI()).navigator.navigateTo("SharedLibrary");
				}        	
	        });
	     
	     if(webSphereSharedLibraryValue > 0){
	    	 for(int i=1; i<webSphereSharedLibraryValue+1; i++){
	    		 form.addComponent(new CollapseableBox(String.valueOf(i),getSharedLibraryEntries(i), false));
	    	 }
	     }
	}
	private Component getSharedLibraryEntries(final int pos) {
		// TODO Auto-generated method stub
		final FormLayout form = new FormLayout();
		//application Name
		form.setMargin(false);
        form.setSpacing(true);
        form.setWidth("100%");
        //form.set
        form.addStyleName("light");
        String websphereSharedLibName=wasConfigProperty.getString("webSphereSharedLibraryName."+pos, "");
        String websphereSharedLibClasspath=wasConfigProperty.getString("webSphereSharedLibraryClasspath."+pos, "");
        
        final TextField websphereSharedLibNameText = new TextField("Name");
        websphereSharedLibNameText.setValue(websphereSharedLibName);
        websphereSharedLibNameText.setWidth("100%");
        websphereSharedLibNameText.addValueChangeListener(new Property.ValueChangeListener(){

   			@Override
   			public void valueChange(ValueChangeEvent event) {
   				// TODO Auto-generated method stub
   				String value = (String) event.getProperty().getValue();
   				((AtfconfiguratorUI) getUI()).getWasConfiguration().setProperty("webSphereSharedLibraryName."+pos, value);
   			}
           	
           });
        websphereSharedLibNameText.setImmediate(true);
        form.addComponent(websphereSharedLibNameText);
        
        final TextField websphereSharedLibClasspathText = new TextField("Classpath");
        websphereSharedLibClasspathText.setValue(websphereSharedLibClasspath);
        websphereSharedLibClasspathText.setWidth("100%");
        websphereSharedLibClasspathText.addValueChangeListener(new Property.ValueChangeListener(){

   			@Override
   			public void valueChange(ValueChangeEvent event) {
   				// TODO Auto-generated method stub
   				String value = (String) event.getProperty().getValue();
   				((AtfconfiguratorUI) getUI()).getWasConfiguration().setProperty("webSphereSharedLibraryClasspath."+pos, value);
   			}
           	
           });
        websphereSharedLibClasspathText.setImmediate(true);
        form.addComponent(websphereSharedLibClasspathText);
				return form;
	}

}
